package com.andy.nature.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.andy.common.extend.setOnIMClickListener
import com.andy.nature.databinding.AdapterShareItemBlueBinding

class RecruitShareAdapter : RecyclerView.Adapter<RecruitShareAdapter.BottomViewHolder>() {

    private var mContext: Context? = null
    private var shareList = mutableListOf<String>()
    private var actionListener: ActionListener? = null
    private var isCircle = true

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        mContext = recyclerView.context
    }

    fun setActionListener(actionListener: ActionListener) {
        this.actionListener = actionListener
    }

    fun setData(count: Int, isCircle: Boolean) {
        this.isCircle = isCircle
        shareList.clear()
        for (i in 1..count) {
            shareList.add("第${i}组")
        }
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return shareList.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BottomViewHolder {
        val binding = AdapterShareItemBlueBinding.inflate(LayoutInflater.from(mContext), parent, false)
        return BottomViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BottomViewHolder, position: Int) {
        val data = shareList[position]
        holder.binding.apply {
            tvTitle.text = data
            tvTitle.setOnIMClickListener {
                actionListener?.click(position, isCircle)
            }
        }
    }

    class BottomViewHolder(val binding: AdapterShareItemBlueBinding) : RecyclerView.ViewHolder(binding.root)

    interface ActionListener {
        fun click(position: Int, isCircle: Boolean)
    }
}